package com.bookcloud.java21.chapter12.泛型接口125;

import java.util.ArrayList;
import java.util.List;

public class UserRepository implements Repository<User> {
  // 存储用户list
  private List<User> userList = new ArrayList<User>();

  @Override
  public void add(User user) {
    userList.add(user);
  }

  @Override
  public User get(int id) {
    for (User user : userList) {
      if (user.getId() == id) {
        return user;
      }
    }
    return null;
  }

  @Override
  public void update(int id, User user) {
    for (User user1 : userList) {
      if (user1.getId() == id) {
        user.setName(user.getName());
        user.setAge(user.getAge());
      }
    }
  }

  @Override
  public void delete(int id) {
    for (User user1 : userList) {
      if (user1.getId() == id) {
        // 从list中删除用户
        userList.remove(user1);
        break;
      }
    }
  }

  @Override
  public List<User> getAll() {
    return userList;
  }
}
